#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-08-22 9:16 上午
# @Author  : yangdy
# @File    : 2020-08-22.679.24点游戏
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def judgePoint24(self, nums: List[int]) -> bool:
        ans = False
        ss = [True for i in range(4)]
        no = []
        for i0 in range(4):
            if ss[i0]:
                ss[i0] = False
                for i1 in range(4):
                    if ss[i1]:
                        ss[i1] = False
                        for i2 in range(4):
                            if ss[i2]:
                                ss[i2] = False
                                for i3 in range(4):
                                    if ss[i3]:
                                        no.append((i0, i1, i2, i3))
                                ss[i2] = True
                        ss[i1] = True
                ss[i0] = True

        po = []
        for p0 in range(4):
            for p1 in range(4):
                for p2 in range(4):
                    po.append((p0, p1, p2))

        def cal(x, o, y):
            if x is None or y is None or o is None:
                return None
            if o == 3 and y == 0:
                return None
            # op = '+-*/'
            # pp = f'{x}{op[o]}{y}'
            # ret = eval(pp)
            if o == 0:
                return x + y
            elif o == 1:
                return x - y
            elif o == 2:
                return x * y
            elif o == 3:
                return x / y
            return None

        for ni in no:
            for pi in po:
                # 1 2 3
                x1 = cal(nums[ni[0]], pi[0], nums[ni[1]])
                x2 = cal(x1, pi[1], nums[ni[2]])
                xx = cal(x2, pi[2], nums[ni[3]])
                # if xx == 24:
                if xx is not None and abs(xx-24) < 1e-6:
                    # print(ni, pi, '123')
                    return True
                # 1 3 2
                x1 = cal(nums[ni[0]], pi[0], nums[ni[1]])
                x2 = cal(nums[ni[2]], pi[2], nums[ni[3]])
                xx = cal(x1, pi[1], x2)
                # if xx == 24:
                if xx is not None and abs(xx - 24) < 1e-6:
                    # print(ni, pi, '132')
                    return True
                # 2 1 3
                x1 = cal(nums[ni[1]], pi[1], nums[ni[2]])
                x2 = cal(nums[ni[0]], pi[0], x1)
                xx = cal(x2, pi[2], nums[ni[3]])
                # if xx == 24:
                if xx is not None and abs(xx - 24) < 1e-6:
                    # print(ni, pi, '213')
                    return True
                # 2 3 1
                x1 = cal(nums[ni[1]], pi[1], nums[ni[2]])
                x2 = cal(x1, pi[2], nums[ni[3]])
                xx = cal(nums[ni[0]], pi[2], x2)
                # if xx == 24:
                if xx is not None and abs(xx - 24) < 1e-6:
                    # print(ni, pi, '231')
                    return True
                # 3 1 2
                x1 = cal(nums[ni[2]], pi[2], nums[ni[3]])
                x2 = cal(nums[ni[0]], pi[0], nums[ni[1]])
                xx = cal(x1, pi[1], x2)
                # if xx == 24:
                if xx is not None and abs(xx - 24) < 1e-6:
                    # print(ni, pi, '312')
                    return True
                # 3 2 1
                x1 = cal(nums[ni[2]], pi[2], nums[ni[3]])
                x2 = cal(nums[ni[1]], pi[1], x1)
                xx = cal(nums[ni[0]], pi[0], x2)
                # if xx == 24:
                if xx is not None and abs(xx - 24) < 1e-6:
                    # print(ni, pi, '321')
                    return True

        return ans


questions = [
    ([4, 1, 8, 7],),
    ([1, 2, 1, 2],),
    ([3, 5, 1, 1],),
    ([3, 4, 6, 7],),
    ([1, 3, 4, 6],),
    ([3, 3, 8, 8],),
    ([1, 5, 5, 5],),
]

work(questions, lambda q: Solution().judgePoint24(q[0]))

# Solution().judgePoint24([3, 5, 1, 1])
